package cn.tedu.egweibo.controller;

import cn.tedu.egweibo.base.respont.JsonResult;
import cn.tedu.egweibo.base.respont.StatusCode;
import cn.tedu.egweibo.mapper.CommentMapper;
import cn.tedu.egweibo.pojo.dto.CommentAddParam;
import cn.tedu.egweibo.pojo.entity.Comment;
import cn.tedu.egweibo.pojo.vo.CommentVO;
import cn.tedu.egweibo.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RequestMapping("/v1/comment/")
@RestController
@Api(tags = "03.评论模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @PostMapping("insert")
    @ApiOperation("发表评论")
    @ApiOperationSupport(order = 100)
    public JsonResult insert(@RequestBody CommentAddParam commentAddParam,
                             @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddParam,comment);
        comment.setUserId(userVO.getId());
        comment.setCreateTime(new Date());
        commentMapper.insert(comment);
        return JsonResult.ok();
    }
    @GetMapping("selectByWeiboId")
    @ApiOperation("获取评论列表")
    @ApiOperationSupport(order = 200)
    public JsonResult selectByWeiboId(Long weiboId){
        List<CommentVO> list= commentMapper.selectByWeiboId(weiboId);
        return JsonResult.ok(list);
    }
}
